package com.huike.report.service.impl;

import com.huike.clues.domain.TbClue;
import com.huike.contract.domain.vo.TransferVo;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public IndexVo getQueryData(LocalDate beginCreateTime, LocalDate endCreateTime) {

        IndexVo indexVo = new IndexVo();
        LocalDateTime beginDate = LocalDateTime.of(beginCreateTime, LocalTime.MIN);
        LocalDateTime endDate = LocalDateTime.of(endCreateTime, LocalTime.MAX);

        HashMap<String, Object> map = new HashMap<>();
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        map.put("FALSE", 4);

        Integer cluesNum = reportMapper.cluesNum(map);
        Integer businessNum = reportMapper.businessNum(map);
        Integer contractNum = reportMapper.contractNum(map);
            Double salesAmount = reportMapper.salesAmount(map);
        if (salesAmount==null){
            indexVo.setSalesAmount(0.0);
        }else{
            indexVo.setSalesAmount(salesAmount);
        }
        LocalDateTime todayBeginDate = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime todayEndDate = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        map.put("todayBeginDate", todayBeginDate);
        map.put("todayEndDate", todayEndDate);
        Integer todayCluesNum = reportMapper.todayCluesNum(map);
        Integer todayBusinessNum = reportMapper.todayBusinessNum(map);
        Integer todayContractNum = reportMapper.todayContractNum(map);
        Double todaySalesAmount = reportMapper.todaySalesAmount(map);
        if (todaySalesAmount==null){
            indexVo.setTodaySalesAmount(0.0);
        }else{
            indexVo.setSalesAmount(todaySalesAmount);
        }
        // TbClue.StatusType.UNFOLLOWED
        map.put("UNFOLLOWED", 1);//待跟进

        Integer tofollowedCluesNum =  reportMapper.tofollowedCluesNum(map);
        Integer tofollowedBusinessNum=reportMapper.tofollowedBusinessNum(map);
        Integer toallocatedCluesNum=reportMapper.toallocatedCluesNum(map);
        Integer toallocatedBusinessNum=reportMapper.toallocatedBusinessNum(map);

        indexVo.setCluesNum(cluesNum);
        indexVo.setBusinessNum(businessNum);
        indexVo.setContractNum(contractNum);

        indexVo.setTodayCluesNum(todayCluesNum);
        indexVo.setTodayBusinessNum(todayBusinessNum);
        indexVo.setTodayContractNum(todayContractNum);
        indexVo.setTofollowedCluesNum(tofollowedCluesNum);
        indexVo.setTofollowedBusinessNum(tofollowedBusinessNum);
        indexVo.setToallocatedCluesNum(toallocatedCluesNum);
        indexVo.setToallocatedBusinessNum(toallocatedBusinessNum);


        return indexVo;
    }
}
